package com.focus.xyz.union.pages.frontend;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BaseForm;
import com.focus.xyz.core.utils.Constants;
import com.focus.xyz.union.data.SubAcctBean;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.util.List;

/**
 * Created by chenmeng on 2016/5/12.
 */
public class SubAcctPage extends BaseForm {
    private Action action;

    @FindBy(className = "btn-new-account")
    private WebElement addSubAcctBtn;
    @FindBy(xpath = "//table[@class='new-table']/tbody")
    private WebElement subAcctTab;
    @FindBy(xpath = "//table[@class='new-sub-account']/tbody/tr/td/input")
    private List<WebElement> subInfoForm;

    /*子账号分配产品*/
    @FindBy(xpath = "//table[@id='dev_dis_prod']/tbody")
    private WebElement assignProdTable;

    @FindBy(xpath = "//tr[@id='dev_table_top']/th/div/input")
    private WebElement assignAllCheckbox;
    @FindBy(id = "dev_submit")
    private WebElement submitBtn;

    public SubAcctPage(WebDriver driver,Action action){
        super(driver,action);
        this.action = action;
        PageFactory.initElements(driver,this);
    }

    public void clickAddBtn() throws Exception{
        action.click(this.addSubAcctBtn);
    }

    @Override
    protected List<WebElement> getAllElements() throws Exception {
        /*for(WebElement e:subInfoForm){
            System.out.println("private String " +e.getAttribute(Constants.ATTR_NAME)+";");
        }*/
        return subInfoForm;
    }

    public void fillSubInfo(SubAcctBean bean, String prodName) throws Exception{
        bind(bean);
        deselectAll();
        selectProd(prodName);
        clickSubmitBtn();
        action.waitForPageLoad();
    }

    public void clickSubmitBtn() throws Exception{
        action.click(submitBtn);
    }
    public void deselectAll() throws Exception{
        action.deselectCheckBox(assignAllCheckbox);
    }

    /*为子账号选择产品*/
    public void selectProd(String prodName) throws Exception{
        List<WebElement> pInfos = assignProdTable.findElements(By.tagName(Constants.TAG_TR));
        for(WebElement e: pInfos){
            List<WebElement> c = e.findElements(By.tagName(Constants.TAG_TD));
            if(prodName.equals(c.get(1).getText().trim())){
                action.click(c.get(0));
                break;
            }
        }
    }


    /*获取子账号的状态*/
    public String  getSubStatus(String key) throws Exception{
        List<WebElement> rows = subAcctTab.findElements(By.tagName(Constants.TAG_TR));
        String status = "";
        if(rows!=null&&rows.size()>1){
            for(int i = 1;i<rows.size();i++){
                List<WebElement> cols = rows.get(i).findElements(By.tagName(Constants.TAG_TD));
                if(key.equals(cols.get(0).getText().trim())){
                  WebElement st =  rows.get(i).findElement(By.xpath("td[@class='dev_sub_status']/span"));
                    status = st.getText().trim();
                    break;
                }
            }
        }
        return status;

    }


}
